package com.boge.modules.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boge.common.utils.ShiroUtils;
import com.boge.modules.house.entity.THouseEntity;
import com.boge.modules.house.entity.THousepjEntity;
import com.boge.modules.house.service.THouseService;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.Query;

import com.boge.modules.house.dao.TCollectDao;
import com.boge.modules.house.entity.TCollectEntity;
import com.boge.modules.house.service.TCollectService;


@Service("tCollectService")
public class TCollectServiceImpl extends ServiceImpl<TCollectDao, TCollectEntity> implements TCollectService {

    @Autowired
    private THouseService houseService;

    @Autowired
    private SysUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity loginUser = ShiroUtils.getUserEntity();
        IPage<TCollectEntity> page = this.page(
                new Query<TCollectEntity>().getPage(params),
                new LambdaQueryWrapper<TCollectEntity>()
                .eq(!"admin".equals(loginUser.getUsername()),TCollectEntity::getUserId,loginUser.getUserId())
        );

        List<TCollectEntity> records = page.getRecords();
        if(records != null){
            for (TCollectEntity record : records) {
                Integer userId = record.getUserId();
                SysUserEntity userEntity = userService.getById(userId);
                record.setUserName(userEntity.getNickname());
                Integer houseId = record.getHouseId();
                THouseEntity houseEntity = houseService.getById(houseId);
                record.setHouseName(houseEntity.getName());
                record.setHouseImg(WebController.BASE_URL+houseEntity.getImg());
            }
        }

        return new PageUtils(page);
    }

}